package com.jc;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.awt.Toolkit;



/**
 * @Author: LZY
 * @Date: 2025/11/04 09:38
 * @Description: 用弹框拼出爱心形状
 **/
public class test4 {
    private static final String[] TIPS = {
            "多喝水哦~", "保持微笑呀", "保持微笑呀",
            "记得吃水果", "保持好心情", "好好爱自己", "我想你了",
            "梦想成真", "期待下一次见面", "金榜题名",
            "顺顺利利", "早点休息", "愿所有烦恼都消失",
            "别熬夜", "过得开心嘛", "天冷了，多穿衣服",
            "今天要开心哦", "今天要努力哦", "今天要加油哦",
//            "重生之我在八维卖黑奴~", "黑奴", "么么哒",
//            "小黑子~", "黑奴傻嘚", "",

    };

    private static final Color[] BG_COLORS = {
            new Color(255, 182, 193),
            new Color(135, 206, 235),
            new Color(144, 238, 144),
            new Color(230, 230, 250),
            new Color(255, 255, 224),
            new Color(221, 160, 221),
            new Color(255, 127, 80),
            new Color(255, 228, 196),
            new Color(127, 255, 212),
            new Color(255, 228, 225),
            new Color(240, 255, 240),
            new Color(255, 240, 245),
            new Color(253, 245, 230),
    };

    // 存储爱心形状的坐标点
    private static List<Point> heartPoints = new ArrayList<>();

    public static void main(String[] args) {
        // 计算爱心形状的坐标点
        calculateHeartPoints();

        ExecutorService executor = Executors.newSingleThreadExecutor();
        Random random = new Random();

        // 按爱心形状的位置显示弹窗
        for (int i = 0; i < heartPoints.size(); i++) {
            final int index = i;
            executor.submit(() -> {
                try {
                    Thread.sleep(50); // 缩短延迟，让爱心更快显示
                    SwingUtilities.invokeLater(() -> createTipWindow(heartPoints.get(index), random));
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
        }
        executor.shutdown();
    }

    /**
     * 计算爱心形状的坐标点
     * 使用参数方程：x = 16sin³(t), y = 13cos(t) - 5cos(2t) - 2cos(3t) - cos(4t)
     */
    private static void calculateHeartPoints() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int centerX = screenSize.width / 2;
        int centerY = screenSize.height / 2;
        int scale = 25; // 缩放因子，控制爱心大小

        // 生成爱心曲线上的点
        for (double t = 0; t <= 2 * Math.PI; t += 0.02) {
            // 爱心参数方程
            double x = 16 * Math.pow(Math.sin(t), 3);
            double y = 13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t);

            // 转换为屏幕坐标（爱心需要翻转Y轴）
            int screenX = (int)(centerX + x * scale);
            int screenY = (int)(centerY - y * scale); // 注意Y轴翻转

            heartPoints.add(new Point(screenX, screenY));
        }
    }

    private static void createTipWindow(Point location, Random random) {
        // 创建窗口
        JFrame frame = new JFrame("❤");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(300, 100); // 窗口大小

        // 设置位置（爱心形状的坐标）
        frame.setLocation(location.x, location.y);

        // 随机背景色，但偏向红色系
        Color bgColor;
        if (random.nextDouble() < 0.7) {
            // 70%概率使用红色系
            bgColor = new Color(255, random.nextInt(100) + 100, random.nextInt(100) + 100);
        } else {
            bgColor = BG_COLORS[random.nextInt(BG_COLORS.length)];
        }

        // 创建标签
        String tip = TIPS[random.nextInt(TIPS.length)];
        if (tip.isEmpty()) {
            tip = "❤";
        }
        JLabel label = new JLabel(tip, SwingConstants.CENTER);
        label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        label.setOpaque(true);
        label.setBackground(bgColor);
        label.setPreferredSize(new Dimension(60, 60));
        frame.add(label);

        // 窗口置顶
        frame.setAlwaysOnTop(true);
        // 无边框更美观（可选）
        frame.setUndecorated(true);
        // 显示窗口
        frame.setVisible(true);
    }
}